package nc.ui.querytemplate;

import nc.bs.logging.Logger;
import nc.itf.uap.qrytemplate.IQTDataQueryService;
import nc.ui.pub.query.cache.Uif2QTCacheUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.query.QueryTempletTotalVO;
import nc.vo.pub.query.QueryTempletVO;
import nc.vo.querytemplate.*;
import nc.vo.util.remotecallcombination.RemoteCallCombinatorEx;
import nc.vo.util.remotecallcombination.Token;

public class QueryTempletLoader
{

    public QueryTempletLoader()
    {
        token = null;
        firstQuery = true;
    }

    public void prepare(TemplateInfo info)
    {
        templateInfo = info;
        dataFromCache = getDataFromCache(templateInfo);
        if(dataFromCache != null)
            templateInfo.setTs(dataFromCache.getTs());
        RemoteCallCombinatorEx rcc = RemoteCallCombinatorEx.getInstance();
        if(token != null)
        {
            rcc.update(token);
            doInvoke(rcc);
        } else
        {
            doInvoke(rcc);
            token = rcc.getToken();
        }
    }

    private QueryTempletTotalVOWithTS getDataFromCache(TemplateInfo templateInfo)
    {
        QueryTempletTotalVOWithTS data = Uif2QTCacheUtil.getQTTVOWithTS(templateInfo);
        if(data != null)
            log("*****Find in Cache*****\n", templateInfo.getCEKey(), templateInfo.getTemplateId(), data.getTs());
        return data;
    }

    private void doInvoke(RemoteCallCombinatorEx rcc)
    {
        IQTDataQueryService queryService = (IQTDataQueryService)rcc.getService(IQTDataQueryService.class);
        try
        {
            queryService.getQueryTempletDataWithTSValidation(templateInfo);
        }
        catch(BusinessException e)
        {
            Logger.error(e.getMessage(), e);
        }
    }

    public QueryTempletData getQueryTempletData()
    {
        if(queryTempletdata == null && firstQuery)
        {
            queryTempletdata = combineResultFromDBAndCache();
            firstQuery = false;
        }
        return queryTempletdata;
    }

    private QueryTempletData combineResultFromDBAndCache()
    {
        QueryTempletData dataFromDB = null;
        try
        {
            dataFromDB = (QueryTempletData)RemoteCallCombinatorEx.getInstance().getResult(token);
            if(dataFromDB != null)
                combinDataInCache(dataFromDB);
        }
        catch(BusinessException e)
        {
            Logger.error("\u6A21\u677F\u52A0\u8F7D\u5931\u8D25!", e);
        }
        return dataFromDB;
    }

    private void combinDataInCache(QueryTempletData data)
    {
        QueryTempletTotalVO qttvo = data.getQueryTempletTotalVO();
        if(qttvo == null)
        {
            data.setQueryTempletTotalVO(dataFromCache.getQueryTempletTotalVO());
        } else
        {
            Uif2QTCacheUtil.cacheQTTVO(templateInfo, qttvo);
            log("*****Find in Cache but TS changed*****\n", data.getTemplateInfo().getCEKey(), qttvo.getTempletVO().getId(), qttvo.getTempletVO().getTs().toString());
        }
    }

    private static void log(String msg, String ceKey, String pk_templet, String ts)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(msg);
        sb.append("\nKEY:");
        sb.append(ceKey);
        sb.append("\nID:");
        sb.append(pk_templet);
        sb.append("\nTS:");
        sb.append(ts);
        Logger.debug(sb.toString());
        Logger.debug(sb.toString());
    }

    private Token token;
    private TemplateInfo templateInfo;
    private QueryTempletTotalVOWithTS dataFromCache;
    private QueryTempletData queryTempletdata;
    private boolean firstQuery;
}